' (c) 2009 Joel Winarske 
' All Rights Reserved

' BSP Vendor
strVendor = "TI"

' Target CPU
str_TGTCPU = "ARMV4I"

' Target project
str_TGTPROJ = "CEBASE"

' WinCE Platform
str_TGTPLAT = "TI_EVM_3530"


' Folder that holds the config files
strConfig = "Configs"

' Target folder that holds shortcuts
strTargetFolder = "Build Windows"

' Registry key used to look up OS Install directory
strRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Platform Builder\6.00\Directories\OS Install Dir"

Set Sh = CreateObject("WScript.Shell")

str_WINCEROOT = Sh.RegRead(strRegKey)

If str_WINCEROOT = "" Then
    WScript.Echo "No registry entry for _WINCEROOT found"
    WScript.Quit
End If


' trim trailing wack
If Right(str_WINCEROOT,1) = "\" Then
    str_WINCEROOT = Left(str_WINCEROOT,Len(str_WINCEROOT)-1)
End If

' Target platform root
str_TARGETPLATROOT = str_WINCEROOT + "\PLATFORM\" + str_TGTPLAT

' trim off drive letter
strOS = Mid(str_WINCEROOT,4,Len(str_WINCEROOT)-3)

' shortcut icon
strIcon = str_WINCEROOT + "\PUBLIC\COMMON\OAK\MISC\wince.ico"



'Create start menu folders for storing short cuts
strFolder = Sh.SpecialFolders("StartMenu") + "\Programs"
strFolder = CreateFolder(strFolder + "\" & strVendor)
strFolder = CreateFolder(strFolder + "\" & str_TGTPLAT)
strFolder = CreateFolder(strFolder + "\" & strTargetFolder)


'Enumerate all batch files in _TGTPLAT\configs and generate a lnk for each
strComputer = "."
Set objWMIService = GetObject("winmgmts:" + _
    "{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")

Dim count
count = 0

Set colFiles = objWMIService. _
    ExecQuery("Select * from CIM_DataFile where Path = '\\" + strOS + _
        "\\PLATFORM\\" + str_TGTPLAT + "\\" + strConfig + "\\' and Extension = 'bat'")

For Each objFile in colFiles
    count = count + 1
    Call CreateShortcut(strFolder, objFile.FileName)
Next

Select Case count
    Case 0
        WScript.Echo "No config batch files present in " + str_TGTPLATROOT + "\" + strConfig
    Case 1
        WScript.Echo "Shortcut was created in " + strFolder
    Case Else
        WScript.Echo count & " Shortcuts were created in:" + vbCRLF + "'" + strFolder + "'"
End Select   


' filename gets passed without extension
Sub CreateShortcut (folder, filename)

     set WshShell = WScript.CreateObject("WScript.Shell")

     Set oShellLink = WshShell.CreateShortcut(folder + "\" + filename + ".lnk")
     oShellLink.TargetPath = "%comspec%"
     oShellLink.Arguments = "/k ""set _WINCEROOT=" + str_WINCEROOT + "&&" + _
          str_WINCEROOT + "\PLATFORM\" + str_TGTPLAT + "\" + strConfig + _
          "\""" + filename + ".bat"" " + _
          str_TGTCPU + " " + str_TGTPROJ + " " + str_TGTPLAT + """"
     oShellLink.IconLocation = strIcon + ", 0"
     oShellLink.WindowStyle = 1
     oShellLink.WorkingDirectory = "%windir%"
     oShellLink.Save

End Sub


Function CreateFolder (fldr)

   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")

   If (fso.FolderExists(fldr)) Then
      CreateFolder = fldr
   Else
      Set f = fso.CreateFolder(fldr)
      CreateFolder = f.Path
   End If

End Function